<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar title="商家登陆" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="code-login-box">
			<view class="code-login-box-top">
				<image src="../../static/login/msjimg.png"></image>
				<view class="code-login-box-top-text">领先的会员平台</view>
			</view>
			<view class="code-login-box-center">
				<view class="code-login-box-center-phone">
					<view class="code-login-box-center-phone-86">+86</view>
					<input type="text" v-model="phone" maxlength="11" placeholder="输入手机号码" class="code-login-box-center-phone-input" placeholder-style="color:#AAAAAA"/>
				</view>
				<view class="code-login-box-center-password">
					<view class="code-login-box-center-password-left">
						<input type="text" maxlength="4" v-model="messageCode" placeholder="输入验证码" placeholder-style="color:#AAAAAA" />
					</view>
					<view class="code-login-box-center-password-right">
						<view class="btns active" @click="getSendSms">{{ getMessageText }}</view>
					</view>
				</view>
				<view class="code-login-box-center-login">
					<button @click="loginFun">登陆</button>
					<text @click="goLoginPage">使用账号密码登录</text>
				</view>
			</view>
			<view class="code-login-box-bottom">
				<view class="code-login-box-bottom-title">
					<view class="code-login-box-bottom-title-xianl"></view>
					<view class="code-login-box-bottom-title-text">快速登录</view>
					<view class="code-login-box-bottom-title-xianr"></view>
				</view>
				<view class="code-login-box-bottom-party-login"><image src="../../static/login/wximg.png"></image></view>
				<view class="code-login-box-bottom-agreement">
					<view>注册即同意<text>《用户协议》</text>和<text>《隐私策略》</text></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { config } from '@/utils/config.js';
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
export default {
	data() {
		return {
			phone: '',
			messageCode: '', // 短信验证码,
			getMessageText: '发送验证码',
			countdown: 60,
			timer: null
		};
	},
	components: {
		uniNavBar
	},
	methods: {
		beginSetTime() {
			if (this.countdown === 0) {
			  this.getMessageText = '发送验证码'
			  this.countdown = 60
			  this.timer = null
			  return
			} else {
			  this.getMessageText = `${this.countdown}s后重发`
			  this.countdown--
			}
			this.timer = setTimeout(() => {
			  this.beginSetTime()
			}, 1000)
		},
		getSendSms() {
			if (this.timer) {
				return false
			}
			if (!this.phone) {
				uni.showToast({
					icon: 'none',
					title: '请填写手机号'
				})
				return false
			}
			if (!(/^1[34578]\d{9}$/.test(this.phone))) {
				uni.showToast({
					title: '手机号码格式错误',
					icon: 'none'
				})
				return false
			}
			this.beginSetTime()
			uni.request({
				url: `${config.api_base_url_tools}sms`,
				method: 'POST',
				data: {
					sender: 'merchant',
					flag: 'login',
					phone: this.phone
				},
				success: ((res) => {
					let { status, data } = res.data
					if (status.flag === true) {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: status.msg,
							icon: 'none'
						});
					}
				}),
				fail: (() => {
					uni.showToast({
						icon: 'none',
						title: '请求失败, 网络异常'
					})
				})
			});
		},
		// 跳转到使用账号和密码登录页面
		goLoginPage() {
			uni.redirectTo({
				url: '/pages/login/login'
			})
		},
		loginFun() {
			if (!this.phone) {
				uni.showToast({
					icon: 'none',
					title: '请填写手机号'
				})
				return false
			}
			if (!this.messageCode) {
				uni.showToast({
					icon: 'none',
					title: '请填写验证码'
				})
				return false
			}
			uni.request({
				url: `${config.api_base_url}merchant/store/login/phone/${this.messageCode}`,
				method: 'POST',
				data: {
					phone: this.phone
				},
				success: ((res) => {
					let { status, data } = res.data
					if (status.flag === true) {
						uni.setStorageSync('h_token', data.token);
						uni.setStorageSync('h_role', data.role);
						uni.setStorageSync('h_memberId', data.memberId);
						uni.switchTab({
							url: '/pages/index/index'
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: status.msg
						})
					}
				}),
				fail: (() => {
					uni.showToast({
						icon: 'none',
						title: '登录失败, 网络异常'
					})
				})
			});
		}
	}
};
</script>

<style>
page {
}
</style>
<style scoped>
.code-login-box {
	border-top: 2rpx solid rgba(0, 0, 0, 0.05);
	padding-bottom: 60rpx;
	box-sizing: border-box;
}

.code-login-box-top {
	padding-top: 88rpx;
	box-sizing: border-box;
}

.code-login-box-top image {
	width: 144rpx;
	height: 144rpx;
	display: block;
	margin: 0px auto;
}

.code-login-box-top-text {
	font-size: 28rpx;
	letter-spacing: 3rpx;
	line-height: 40rpx;
	color: rgba(36, 36, 36, 1);
	text-align: center;
	margin-top: 38rpx;
}

.code-login-box-center {
	padding: 130rpx 60rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-phone {
	border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
	display: flex;
	align-items: center;
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-phone-86 {
	height: 44rpx;
	font-size: 32rpx;
	font-weight: 600;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
	width: 140rpx;
}

.code-login-box-center-phone-input {
	height: 44rpx;
	font-size: 32rpx;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
}

.code-login-box-center-password {
	padding: 20rpx 0rpx;
	box-sizing: border-box;
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.code-login-box-center-password-left {
	border-bottom: 2rpx solid rgba(0, 0, 0, 0.05);
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-password-left input {
	height: 44rpx;
	font-size: 32rpx;
	color: rgba(36, 36, 36, 1);
	line-height: 44rpx;
}

.code-login-box-center-password-right {
	padding: 20rpx 0rpx;
	box-sizing: border-box;
}

.code-login-box-center-password-right button {
	line-height: 40rpx;
	height: 40rpx;
	font-size: 28rpx;
	width: 140rpx;
	background: #fff;
	border: none;
	padding: 0px;
	box-sizing: border-box;
	color: #aaaaaa;
}

.code-login-box-center-password-right button::after {
	border: none;
}

.code-login-box-center-login {
}

.code-login-box-center-login button {
	width: 482rpx;
	height: 84rpx;
	background: rgba(240, 34, 48, 1);
	border-radius: 42rpx;
	font-size: 28rpx;
	line-height: 84rpx;
	color: rgba(255, 255, 255, 1);
	margin: 108rpx auto 48rpx;
	letter-spacing: 4rpx;
}

.code-login-box-center-login text {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(36, 36, 36, 1);
	line-height: 36rpx;
	text-align: center;
	width: 100%;
	display: block;
	letter-spacing: 3rpx;
}

.code-login-box-bottom {
	margin-top: 40rpx;
}

.code-login-box-bottom-title {
	display: flex;
	justify-content: center;
	align-items: center;
}

.code-login-box-bottom-title-xianl {
	width: 284rpx;
	height: 2rpx;
	background: rgba(0, 0, 0, 0.05);
	margin-top: 2rpx;
}

.code-login-box-bottom-title-text {
	width: 120rpx;
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(170, 170, 170, 1);
	line-height: 36rpx;
	text-align: center;
}

.code-login-box-bottom-title-xianr {
	width: 284rpx;
	height: 2rpx;
	background: rgba(0, 0, 0, 0.05);
	margin-top: 2rpx;
}

.code-login-box-bottom-party-login {
}

.code-login-box-bottom-party-login image {
	width: 68rpx;
	height: 68rpx;
	display: block;
	margin: 46rpx auto 68rpx;
}

.code-login-box-bottom-agreement {
	text-align: center;
}

.code-login-box-bottom-agreement view {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(170, 170, 170, 1);
	line-height: 36rpx;
}

.code-login-box-bottom-agreement view text {
	height: 36rpx;
	font-size: 24rpx;
	font-weight: 400;
	color: rgba(102, 172, 255, 1);
	line-height: 36rpx;
}

.btns {
	width: 200rpx;
	background-color: #f2f2f2;
	font-size: 30rpx;
	text-align: center;
	line-height: 84rpx;
	border-radius: 84rpx;
	color: #fff;
}

.btns.active {
	background-color: #f02230;
}
</style>
